package com.oa.co.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.oa.co.common.PageVo;
import com.oa.co.common.R;
import com.oa.co.listener.WebStaffListener;
import com.oa.co.pojo.LeaveInformation;
import com.oa.co.service.LeaveInformationAdminService;
import com.oa.co.vo.DepartmentBarVo;
import com.oa.co.vo.DepartmentPieVo;
import com.oa.co.vo.LeaveInformationVo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author zhao chen qi
 * @date 2023/5/8 17:15
 */
@RestController
@RequestMapping("/ajax")
public class LeaveInformationAdminController {

    @Autowired
    LeaveInformationAdminService leaveInformationAdminService;
//    查询所有学生 要两表联查 和 模糊查询同时具备
    @RequestMapping("/staffLeaveRequestList")

    public PageVo<LeaveInformation> staffLeaveRequestList(String applicant, String subordinate_department,Integer page, Integer limit){
        System.out.println(111);
        PageVo<LeaveInformation> pageVo = new PageVo<>();

         PageInfo<LeaveInformation> pageData = leaveInformationAdminService.selectStaffLeaveRequestList(applicant,subordinate_department,page,limit);

        pageVo.setCode(0);

        pageVo.setCount(pageData.getTotal());
        pageVo.setData(pageData.getList());
        pageVo.setMsg("员工请假分页数据");

        return pageVo;
    }

    @RequestMapping("/addLeaveInformationData")
    public R addStaffLeaveRequest(@RequestBody LeaveInformation leaveInformation){
        leaveInformation.setD_id("QJ-"+System.currentTimeMillis());
        int result = leaveInformationAdminService.addLeaveInformationData(leaveInformation);
        if (result > 0){

        return R.ok();
        }else {
            return R.error();
        }
    }

    @RequestMapping("/editStaff")
    public R editStaff(LeaveInformation leaveInformation){
        int result = leaveInformationAdminService.editStaff(leaveInformation);
        if (result > 0){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @RequestMapping("/deleteStaff")
    public R deleteStaff(Integer id){
        int result = leaveInformationAdminService.deleteStaff(id);
        if (result > 0){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }
    @RequestMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        下载Excel模板文件
//        设置下载excel的响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
//        得到这个文件的真实路径
        String realPath = request.getServletContext().getRealPath("/staffwriter.xlsx");

        File file = new File(realPath);

//        借助commons-io的工具类 把 file 对象转为字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);

//        防止中文乱码
        String filename = URLEncoder.encode("员工请假模板", "utf-8");

//        告诉浏览器 你要下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

//        得到一个指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();
//        输出流 把字节数组 输出到浏览器
        outputStream.write(bytes);
//        关闭流
        outputStream.close();


    }
    @RequestMapping("/export")
    public void export(HttpServletRequest request, HttpServletResponse response,String applicant, String subordinate_department,Integer page, Integer limit) throws IOException {
//        下载excel模板文件
//        设置下载excel的响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        //防止中文乱码
        String filename = URLEncoder.encode("员工信息导出", "utf-8");

        //告诉浏览器  你要下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

//        得到一个指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, LeaveInformationVo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();
//        System.out.println(1111);
//        准备列表数据
//        PageInfo<LeaveInformation> pageData = leaveInformationAdminService.selectStaffLeaveRequestList(applicant,subordinate_department,page,limit);
        PageInfo<LeaveInformationVo> pageData = leaveInformationAdminService.selectStaffLeaveRequestList2(applicant,subordinate_department,page,limit);
//        System.out.println(pageData);
        sheet.doWrite(pageData.getList());

    }
    @Autowired
    private WebStaffListener webStaffListener;

    //上传excel 文件  读取excel 文件中的数据 存到数据库中
    @RequestMapping("/import")
    public R readExcel(MultipartFile file) throws IOException {

        ExcelReaderBuilder builder = EasyExcel.read(file.getInputStream(), LeaveInformationVo.class, webStaffListener);

        ExcelReaderSheetBuilder sheet = builder.sheet();

        sheet.doRead();

        return R.ok();
    }

    @RequestMapping("/bar")
    public R echarBar(){
        List<DepartmentBarVo> list = leaveInformationAdminService.getBarData();
        return R.ok().data("barData",list);
    }

    @RequestMapping("/pie")
    public R echarPie(){
        List<DepartmentPieVo> list = leaveInformationAdminService.getPieData();
        return R.ok().data("pieData",list);
    }
}
